package com.pilipili.controller;

import com.alibaba.fastjson.JSON;
import com.pilipili.pojo.Books;
import com.pilipili.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
public class BookController {
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    @RequestMapping("/insertBook")
    public String insertBook(Model model,int bookID,String bookName,int bookCounts,String type,String detail){
        model.addAttribute("bookID",bookID);
        model.addAttribute("bookName",bookName);
        model.addAttribute("bookCounts",bookCounts);
        model.addAttribute("type",type);
        model.addAttribute("detail",detail);
        Books books = new Books(bookID, bookName, bookCounts, type, detail);
        int i = bookService.insertBook(books);
        if(i>0){
            return "Book";
        }else {
            model.addAttribute("insertError","添加失败,请重试!");
            return "insertBook";
        }
    }
    @RequestMapping("/toInsertBook")
    public String toInsertBook(){
        return "insertBook";
    }
    @RequestMapping("/updateBook")
    public String updateUser(Model model,int bookID,String bookName,int bookCounts,String type,String detail){
        model.addAttribute("bookID",bookID);
        model.addAttribute("bookName",bookName);
        model.addAttribute("bookCounts",bookCounts);
        model.addAttribute("type",type);
        model.addAttribute("detail",detail);
        return "updateBook";
    }
    @PostMapping("toUpdateBook")
    public String toUpdateUser(Model model, HttpServletRequest request, HttpServletResponse response){
        Integer bookID = Integer.valueOf(request.getParameter("bookID"));
        String bookName = request.getParameter("bookName");
        Integer bookCounts = Integer.valueOf(request.getParameter("bookCounts"));
        String type = request.getParameter("type");
        String detail = request.getParameter("detail");

        Books books = new Books(bookID, bookName, bookCounts, type, detail);
        int updateBook = bookService.updateBook(books);

        if (updateBook>0){
            return "Book";
        }else {
            model.addAttribute("updateTs","修改失败,请稍后重试!");
            return "updateBook";
        }
    }
    @RequestMapping("/Borrow")
    public String Borrow(Model model){
        int AllCount = bookService.selectBookCount(null);
        int wxlCount = bookService.selectBookCount("文学类");
        int dmlcount = bookService.selectBookCount("动漫类");
        int kjlcount = bookService.selectBookCount("科技类");
        model.addAttribute("AllCount",AllCount);
        model.addAttribute("wxlCount",wxlCount);
        model.addAttribute("dmlcount",dmlcount);
        model.addAttribute("kjlcount",kjlcount);
        return "borrow";
    }
    @RequestMapping("/deleteBook")
    @ResponseBody
    public String  deleteBook(Model model,int bookID){
        System.out.println("开始删除");
        int i = bookService.deleteBook(bookID);
        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
        //map.put("code",0);
        map.put("msg","200");
        //map.put("count",1);
        map.put("data",i);
        String s = JSON.toJSONString(map);
        System.out.println(s);
        return s;
    }

    @RequestMapping("/journal")
    public String journal(){
        return "journal";
    }
    @RequestMapping ("/selectlBookbyName")
    @ResponseBody
    public String byName(Model model,String bookName) {
        Books book = new Books();
       // String bookName = request.getParameter("bookName");
        book.setBookName(bookName);
        System.out.println(bookName);
        List<Books> books = new ArrayList<>();
        books = bookService.selectBook(book);
        model.addAttribute("list",books);
        int size = books.size();
        model.addAttribute("size",size);
        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",books);
        String s = JSON.toJSONString(map);
        System.out.println(s);
        return s;
    }
    @RequestMapping("/wxl")
    public String selectwxlBook(Model model){
        return "wxlBook";
    }
    @RequestMapping("/wxlBook")
    @ResponseBody
    public String wxl(Model model){
        Books book = new Books();
        book.setType("文学类");
        List<Books> books = new ArrayList<>();
        books = bookService.selectBook(book);
        model.addAttribute("list",books);
        int size = books.size();
        model.addAttribute("size",size);
        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",books);
        String s = JSON.toJSONString(map);
        //System.out.println(s);
        return s;
    }
    @RequestMapping("/dml")
    public String selectdmlBook(Model model){
        return "dmlBook";
    }
    @RequestMapping("/dmlBook")
    @ResponseBody
    public String dml(Model model){

        Books book = new Books();
        book.setType("动漫类");

        List<Books> books = new ArrayList<>();
        books = bookService.selectBook(book);
        model.addAttribute("list",books);
        int size = books.size();
        model.addAttribute("size",size);
        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",books);
        String s = JSON.toJSONString(map);
        //System.out.println(s);
        return s;
    }
    @RequestMapping("/kjl")
    public String selectkjlBook(Model model){
        return "kjlBook";
    }
    @RequestMapping("/kjlBook")
    @ResponseBody
    public String kjl(Model model){

        Books book = new Books();
        book.setType("科技类");

        List<Books> books = new ArrayList<>();
        books = bookService.selectBook(book);
        model.addAttribute("list",books);
        int size = books.size();
        model.addAttribute("size",size);
        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",books);
        String s = JSON.toJSONString(map);
       // System.out.println(s);
        return s;
    }
    @RequestMapping("/selectAllBook")
    public String selectAllBook(Model model){
        return "Book";
    }
    @RequestMapping("/Book")
    @ResponseBody
    public String Book(Model model){
        List<Books> books = new ArrayList<>();
         books = bookService.selectAllBook();
      model.addAttribute("list",books);
        int size = books.size();
        model.addAttribute("size",size);
        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",books);
        String s = JSON.toJSONString(map);
       // System.out.println(s);
        return s;
    }
}
